package cn.kust.apiuser.controller;

import cn.kust.microtourismcommon.constant.TokenConstants;
import cn.kust.microtourismcommon.pojo.dto.ResponseResult;
import cn.kust.apiuser.service.IUserService;
import cn.kust.microtourismcommon.pojo.dto.TokenResult;
import cn.kust.microtourismcommon.pojo.dto.UserNameAvatar;
import cn.kust.microtourismcommon.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author WY
 * @data 2022/7/20 20:59
 **/
@RestController
@RequestMapping("/api-user")
@Slf4j
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/users")
    public ResponseResult getUser(HttpServletRequest request){

        TokenResult tokenResult = JwtUtils.parseRequestToken(request);
        log.debug(tokenResult.getIdentity());
        log.debug(tokenResult.getPhone());
        log.debug(tokenResult.getUserId());



        //从http请求中获取accessToken
        String accessToken = request.getHeader(TokenConstants.TOKEN_REQUEST_KEY);

        // 根据accessToken解析出内容

        return userService.getUserByAccessToken(accessToken);

    }


    @GetMapping("/users/{userId}")
    public ResponseResult getUserById(@PathVariable("userId") String userId, HttpServletRequest request){


        return userService.getUserById(userId);
    }



}
